package com.imooc.miaosha.rabbitmq;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {

    public static final String DIRECT_EXCHANGE = "direct_exchange";
    public static final String MIAOSHA_QUEUE = "miaosha_queue";

    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE);
    }
    @Bean
    public Queue miaoshaQueue(){
        return new Queue(MIAOSHA_QUEUE,true);
    }
    @Bean
    public Binding directBinding(){
        return BindingBuilder.bind(miaoshaQueue()).to(directExchange()).with("");
    }

    /*public static final String QUEUE_NAME1 = "queue1";
    public static final String QUEUE_NAME2 = "queue2";
    public static final String QUEUE_NAME3 = "queue3";

    public static final String TOPIC_EXCHANGE = "topic_exchange";
    public static final String FANOUT_EXCHANGE = "fanout_exchange";
    public static final String HEADERS_EXCHANGE = "headers_exchange";

    @Bean
    public Queue queue1(){
        return new Queue(QUEUE_NAME1,true);
    }
    @Bean
    public Queue queue2(){
        return new Queue(QUEUE_NAME2,true);
    }
    @Bean
    public Queue queue3(){
        return new Queue(QUEUE_NAME3,true);
    }

    //Topic模式
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPIC_EXCHANGE);
    }
    //下面实现的是，将队列和交换机用key绑定，只有带有特定的key才能进入特定的队列
    @Bean
    public Binding topicBinding1(){
        return BindingBuilder.bind(queue1()).to(topicExchange()).with("topicKey1");
    }
    @Bean
    public Binding topicBinding2(){
        return BindingBuilder.bind(queue2()).to(topicExchange()).with("topicKey2");
    }

    //Fanout模式
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(FANOUT_EXCHANGE);
    }
    @Bean
    public Binding fanoutBanding1(){
        return BindingBuilder.bind(queue1()).to(fanoutExchange());
    }
    @Bean
    public Binding fanoutBanding2(){
        return BindingBuilder.bind(queue2()).to(fanoutExchange());
    }
    @Bean
    public Binding fanoutBanding3(){
        return BindingBuilder.bind(queue3()).to(fanoutExchange());
    }

    //Header模式
    @Bean
    public HeadersExchange headersExchange(){
        return new HeadersExchange(HEADERS_EXCHANGE);
    }
    @Bean
    public Binding headersBinding1(){
        Map<String,Object> map = new HashMap<>();
        map.put("headers1","value1");
        map.put("0",0);
        return BindingBuilder.bind(queue1()).to(headersExchange()).whereAll(map).match();
    }
    @Bean
    public Binding headersBinding2(){
        Map<String,Object> map = new HashMap<>();
        map.put("1",1);
        return BindingBuilder.bind(queue2()).to(headersExchange()).whereAll(map).match();
    }
    @Bean
    public Binding headersBinding3(){
        Map<String,Object> map = new HashMap<>();
        map.put("0",0);
        return BindingBuilder.bind(queue3()).to(headersExchange()).whereAll(map).match();
    }*/

}
